#include "drawsim.h"


DrawSim::DrawSim(QWidget *parent): QWidget(parent)
{
    status = 1; // ethernet initialized to colision
    for(int i = 0; i < 99; i++)
    {
        sendRecvIdle[i] = 0;
    }
    masters = 0;
    columns = 8;
    zoom = 10;
    x = 15;
    y = 15;
    setPalette( QPalette( QColor( 250, 250, 250) ) );
    repaint();
}


// does all the actual drawing
void DrawSim::paintEvent( QPaintEvent * )
{

    QPainter p(this);
    p.setPen(Qt::black);
    p.setBrush(Qt::gray);
    if(masters < 20)
    {
        columns = 5;
    }
    else if(masters < 60)
    {
        columns = 8;
    }
    else
    {
        columns = 14;
    }
    //draw the network
// horizontal line

    if( status == 2){
       p.setBrush(Qt::yellow); //busy
    }
    else if( status == 1){
       p.setBrush(Qt::red);  //colision
    }
    else{
       p.setBrush(Qt::blue); //idle
    }

    for( int k = 0 ; k < (masters/columns) ; k++)
    {
        for(int i = 0 ; i < (columns-1) ; i++)
        {


            p.drawRect((x+(3*zoom)) + (i*20*zoom), (y+(3*zoom)) + (k*20*zoom), 20*zoom, 2*zoom);
        }
    }
    for(int i = 0 ; i < (masters%columns -1) ; i++)
    {

        p.drawRect((x+(3*zoom)) + (i*20*zoom), (y+(3*zoom)) + ((masters/columns)*20*zoom), 20*zoom, 2*zoom);
    }
// vertical lines
    for( int k = 1 ; k < (masters/columns) ; k++)
    {
        for(int i = 0 ; i < columns ; i++)
        {

            p.drawRect((x+(3*zoom)) + (i*20*zoom), (y-(20*zoom)) + (k*20*zoom), 2*zoom, 20*zoom);
        }
    }
    if(masters > columns)
    {
        for(int i = 0 ; i < (masters%columns ) ; i++)
        {

            p.drawRect((x+(3*zoom)) + (i*20*zoom), (y-(20*zoom)) + ((masters/columns)*20*zoom), 2*zoom, 20*zoom);
        }
    }


    // draw the monitors
    for( int k = 0 ; k < (masters/columns) ; k++)
    {
        for(int i = 0 ; i < columns ; i++)
        {
            p.setBrush(Qt::gray);
            p.drawRect(x + (i*20*zoom), y + (k*20*zoom), 10*zoom, 10*zoom);

            if( sendRecvIdle[(k*columns)+i] == 2){
                p.setBrush(Qt::green);  //sending
            }
            else if( sendRecvIdle[(k*columns)+i] == 1){
                p.setBrush(Qt::yellow); //receiving
            }
            else if( sendRecvIdle[(k*columns)+i] == 3){
                p.setBrush(Qt::yellow); //checking
            }
            else{
                p.setBrush(Qt::black);  // idle
            }

            p.drawRect((x+(1*zoom)) + (i*20*zoom), (y+(1*zoom)) + (k*20*zoom), 8*zoom, 8*zoom);
        }
    }
    for(int i = 0 ; i < (masters%columns) ; i++)
    {
        p.setBrush(Qt::gray);
        p.drawRect(x + (i*20*zoom), y + ((masters/columns)*20*zoom), 10*zoom, 10*zoom);

        if( sendRecvIdle[(masters)+i] == 2){
            p.setBrush(Qt::green);   //sending
        }
        else if( sendRecvIdle[(masters)+i] == 1){
            p.setBrush(Qt::yellow);   //receiving
        }
        else if( sendRecvIdle[(masters)+i] == 3){
            p.setBrush(Qt::yellow); //checking
        }
        else{
            p.setBrush(Qt::black);   // idle
        }
        p.drawRect((x+(1*zoom)) + (i*20*zoom), (y+(1*zoom)) + ((masters/columns)*20*zoom), 8*zoom, 8*zoom);
    }

}

//slot that redraws ethernet status
void DrawSim::ethernetStatus(int status)
{
    DrawSim::status = status;
    repaint();
}


//slot that redraws when a master does something
void DrawSim::masterAction(int masterIndex, int action)
{
    sendRecvIdle[masterIndex] = action;
    repaint();
}

// redraws when the number of masters changes
void DrawSim::changeMasters(int masters)
{
    DrawSim::masters = masters;
    repaint();
}

//redraws on a zoom change
void DrawSim::changeZoom(int zoom)
{
    DrawSim::zoom = zoom;

    repaint();
}

void DrawSim::setPen(const QPen &pen)
 {
     this->pen = pen;
     update();
 }

void DrawSim::setBrush(const QBrush &brush)
{
    this->brush = brush;
    update();
}
